package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.HotArticleService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.mess.ArticleVisitStreamMess;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

//热门文章实时更新
@Component
public class ArticleHotUpdateListener {

    @Autowired
    private HotArticleService hotArticleService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
    public void onMessage(String msg) {
        //参数校验
        if (StringUtils.isBlank(msg)) {
            return;
        }

        ArticleVisitStreamMess mess = JSON.parseObject(msg, ArticleVisitStreamMess.class);
        hotArticleService.updateScore(mess);

        System.out.println("实时更新分值完成");
    }
}